package com.autumn.sms.service.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.autumn.sms.service.SmsSendException;

/**
 * 服务标
 * 
 * @author 老码农
 *
 *         2018-01-12 13:10:00
 */
public class StandardTemplateValue extends AbstractSmsTemplateValue {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4199800755281459973L;

	private Map<String, StandardTemplateParameterValue> parameters;
	private List<PlatformTemplateValue> platforms;

	private Long templateId;

	/**
	 * 是否限制发送频率
	 */
	private boolean isRestrict;
	/**
	 * 发送频率间隔秒
	 */
	private Integer restrictSeconds;
	/**
	 * 是否是短信验证码
	 */
	private boolean isVerificationCode;
	/**
	 * 短信验证码类型
	 */
	private Integer verificationCodeType;

	public StandardTemplateValue() {
		this.setParameters(new HashMap<>());
		this.setPlatforms(new ArrayList<>());
	}

	public boolean getIsVerificationCode() {
		return isVerificationCode;
	}

	public void setIsVerificationCode(boolean isVerificationCode) {
		this.isVerificationCode = isVerificationCode;
	}

	public Integer getVerificationCodeType() {
		return verificationCodeType;
	}

	public void setVerificationCodeType(Integer verificationCodeType) {
		this.verificationCodeType = verificationCodeType;
	}

	public boolean getIsRestrict() {
		return isRestrict;
	}

	public void setIsRestrict(boolean isRestrict) {
		this.isRestrict = isRestrict;
	}

	public Integer getRestrictSeconds() {
		return restrictSeconds;
	}

	public void setRestrictSeconds(Integer restrictSeconds) {
		this.restrictSeconds = restrictSeconds;
	}

	public Map<String, StandardTemplateParameterValue> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, StandardTemplateParameterValue> parameters) {
		this.parameters = parameters;
	}

	public List<PlatformTemplateValue> getPlatforms() {
		return platforms;
	}

	public void setPlatforms(List<PlatformTemplateValue> platforms) {
		this.platforms = platforms;
	}

	public Long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}

	/**
	 * 
	 * @param requestMap
	 *
	 */
	public void check(Map<String, String> requestMap) {
		if (requestMap == null) {
			requestMap = new HashMap<>(16);
		}
		Map<String, String> checkMap = new HashMap<>(requestMap.size());
		for (Map.Entry<String, String> entry : requestMap.entrySet()) {
			if (entry.getKey() != null && entry.getValue() != null) {
				checkMap.put(entry.getKey().toUpperCase(), entry.getValue());
			}
		}
		for (StandardTemplateParameterValue item : parameters.values()) {
			if (item.getIsMust()) {
				if (checkMap.get(item.getName().toUpperCase()) == null) {
					throw new SmsSendException(false, false,
							" 模板 " + this.getTemplateCode() + " 必须拥有参数 " + item.getName() + " 的值。");
				}
			}
		}
	}
}
